package com.zhongshengnetwork.rightbe;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
//import android.support.v4.app.NotificationCompat;
import android.util.Log;

import androidx.core.app.NotificationCompat;

import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.common.RootMessage;

import io.rong.push.notification.PushMessageReceiver;
import io.rong.push.notification.PushNotificationMessage;

/**
 * Created by zhongsheng-luojianlan on 16/12/17.
 */

public class RCNotificationReceiver extends PushMessageReceiver {
    @Override
    public boolean onNotificationMessageArrived(Context context, PushNotificationMessage message) {
        //showNotifictionIcon(context,message);
        return false;
    }
    @Override
    public boolean onNotificationMessageClicked(Context context, PushNotificationMessage message) {
        if (CommonUtils.isAppAlive(context,"com.zhongshengnetwork.rightbe")){
            //Log.e("点击通知","还活着");
            if (CustomApplication.loginModel == null){
                Intent mainIntent = new Intent(context, MainActivity.class);
                mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mainIntent.putExtra(RootMessage.rootmessage,RootMessage.chatlist_message);
                context.startActivity(mainIntent);
            }else {
                Intent mainIntent = new Intent(context, MainActivity.class);
                mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mainIntent.putExtra(RootMessage.rootmessage,RootMessage.chatlist_message);
                context.startActivity(mainIntent);
            }

        }else {
            Intent launchIntent = context.getPackageManager().
                    getLaunchIntentForPackage("com.zhongshengnetwork.rightbe");
            launchIntent.setFlags(
                    Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            launchIntent.putExtra(RootMessage.rootmessage,RootMessage.chatlist_message);
            context.startActivity(launchIntent);
        }
        return true;
    }

    public static void showNotifictionIcon(Context context, PushNotificationMessage message) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        Intent intent = new Intent(context, MainActivity.class);//将要跳转的界面
        builder.setAutoCancel(true);//点击后消失
        builder.setSmallIcon(R.mipmap.icon);//设置通知栏消息标题的头像
        builder.setDefaults(NotificationCompat.DEFAULT_SOUND);//设置通知铃声
        builder.setTicker(message.getSenderName());
        builder.setContentText(message.getPushContent());//通知内容
        builder.setContentTitle(message.getPushTitle());
        //利用PendingIntent来包装我们的intent对象,使其延迟跳转
        PendingIntent intentPend = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        builder.setContentIntent(intentPend);
        NotificationManager manager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
        manager.notify(0, builder.build());
    }

}
